AtomicFile

open class AtomicFile(source)

Static library support version of the framework's android.util.AtomicFile, a helper class for performing atomic operations on a file by writing to a new file and renaming it into the place of the original file after the write has successfully completed.

Atomic file guarantees file integrity by ensuring that a file has been completely written and sync'd to disk before renaming it to the original file. Previously this is done by renaming the original file to a backup file beforehand, but this approach couldn't handle the case where the file is created for the first time. This class will also handle the backup file created by the old implementation properly.

Atomic file does not confer any file locking semantics. Do not use this class when the file may be accessed or modified concurrently by multiple threads or processes. The caller is responsible for ensuring appropriate mutual exclusion invariants whenever it accesses the file.

Constructors

Link copied to clipboard
constructor(@NonNull baseName: File)
Create a new AtomicFile for a file located at the given File path.

Functions

Link copied to clipboard
open fun delete()
Delete the atomic file.
Link copied to clipboard
Call when you have failed for some reason at writing to the stream returned by startWrite.
Link copied to clipboard
Call when you have successfully finished writing to the stream returned by startWrite.
Link copied to clipboard
Return the path to the base file.
Link copied to clipboard
Open the atomic file for reading.
Link copied to clipboard
A convenience for openRead that also reads all of the file contents into a byte array which is returned.
Link copied to clipboard
Start a new write operation on the file.